﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Projekt
{
    /// <summary>
    /// Beszalitokkal kapcsolatos muveletek
    /// </summary>
    class Firm 
    {
        Dalgen conn = Dalgen.Instance;

        /// <summary>
        /// Hozzaad egy uj ceget az adatbazishoz
        /// </summary>
        /// <param name="CNev">Ceg neve</param>
        /// <param name="CegID">Ceg azonositoja(cui)</param>
        /// <param name="CCim">Ceg cime</param>
        /// <param name="CTel">ceg tel szama</param>
        /// <param name="ErrorMsg">hiba uzenet</param>
        /// <returns>true ha sikerult, kulonben false</returns>
        public bool add(String CNev,String CegID, String CCim, String CTel, ref String ErrorMsg) 
        {
            String sqlQuery = "Insert into Cegek" + "(CegID,CNev,CCim,CTel) values ('"+ CegID + "','" + CNev + "','" + CCim + "','" + CTel + "');";
            return conn.nonQueryCmd(sqlQuery);
        }

        /// <summary>
        /// Lekeri a cegek listajat
        /// </summary>
        /// <returns>Az adatbazisban levo cegek listaja(DataTable)</returns>
        public DataTable getFirmList()
        {
            try
            {
                string sqlQuery = "Select * From Cegek";
                return conn.QueryCmd(sqlQuery);
            }
            catch (Exception es) { throw (es); }
        }

        /// <summary>
        /// A parameterben kapott szurok alapjan keres a cegek kozott
        /// </summary>
        /// <param name="FirmID">Ceg azonositoja</param>
        /// <param name="FirmName">Ceg Neve</param>
        /// <returns>A filtereknek megfelelo cegek adatai(DataTable)</returns>
        public DataTable SearchFirm(String FirmID, String FirmName)
        {
            try
            {
                string sqlQuery = "Select * From Cegek Where CegID like '" + FirmID + "%' And CNev like '" + FirmName + "%'";
                return conn.QueryCmd(sqlQuery);
            }
            catch (Exception es) { throw (es); }
        }

        /// <summary>
        /// Torol egy ceget az adatbazisbol
        /// </summary>
        /// <param name="CegID">Ceg azonositoja</param>
        /// <returns>true ha sikerult, kulonben false</returns>
        public bool DeleteFirm(String CegID)
        {
            try
            {
                string sqlQuery = "Delete from Cegek where CegID = " + CegID;
                return (conn.nonQueryCmd(sqlQuery));
            }
            catch (Exception es) { throw es; }
        }

        /// <summary>
        /// Ceg szerkesztese az adatbazisban
        /// </summary>
        /// <param name="FirmID">ceg azonositoja</param>
        /// <param name="Name">ceg neve</param>
        /// <param name="Number">ceg tel. szama</param>
        /// <param name="Address">ceg cime</param>
        /// <returns>true ha sikerult, kulonben false</returns>
        public bool EditFirm(String FirmID, String Name, String Number, String Address)
        {
            try
            {
                string sqlQuery = "Update Cegek Set CNev = '" + Name + "', CTel = '" + Number + "', CCim = '" + Address + "' WHERE CegID = " + FirmID;
                return conn.nonQueryCmd(sqlQuery);
            }
            catch (Exception es) { throw (es); }
        }
    }
}
